package com.sgb.goods.view.activity.details.order;

import android.view.Gravity;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.contract.view.widget.TagBuilder;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityContractListBinding;
import com.sgb.goods.entity.ContractInfoEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.ContractListModel;
import com.swgk.core.dialog.RoundDialog;

import static com.blankj.utilcode.util.SnackbarUtils.dismiss;

/**
 * 合同信息
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_CONTRACT_LIST)
public class ContractListActivity extends BaseActivity<GoodsActivityContractListBinding, ContractListModel>
        implements SmartRefreshLayoutHelper {

    private String orderNo = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_contract_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ContractListModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("合同信息");
        orderNo = getIntent().getStringExtra("id");
        mViewModel.findContractPage(orderNo, false);
    }

    @Override
    protected void initObserve() {
        mViewModel.contractRecords.observe(this, orderList -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.adapter, orderList);
        });
        // 显示合同状态变更的alert弹窗
        mViewModel.showAlert.observe(this, showAlert -> {
            new RoundDialog.Builder(ContractListActivity.this)
                    .title("提示")
                    .content(showAlert)
                    .confirm("知道了").confirmCallback(v -> mViewModel.findContractPage(orderNo, false)).show();
        });
        // 显示合同无内容弹窗
        mViewModel.showNoContentAlert.observe(this, showNoContentAlert -> {
            RoundDialog.Builder builder = new TagBuilder(ContractListActivity.this)
                    .setTag("checkStatusByNowStatus")
                    .title("温馨提示")
                    .content("当前合同暂无内容\n请在电脑端编辑后再操作！")
                    .contentGravity(Gravity.CENTER)
                    .cancelable(false)
                    .confirmCallback(v -> dismiss());
            builder.singleBtn(true);
            builder.confirm("知道了");
            builder.show();
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mViewModel.findContractPage(orderNo, false);
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.findContractPage(orderNo, false);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.findContractPage(orderNo, true);
            }
        });
        // 查看合同
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.see_contract) {
                ContractInfoEntity.Records record = (ContractInfoEntity.Records) view.getTag();
                mViewModel.checkContract(record);
            }
        });
    }
}
